/**
 * 初始化题库分类详情对话框
 */
var QuestionCategoryInfoDlg = {
    questionCategoryInfoData: {},
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '分类名称不能为空'
                }, stringLength: {
                    min: 1,
                    max: 100,
                    message: '分类名称字数必须在1到100之间'
                }
            }
        },
        pidSel: {
            validators: {
                notEmpty: {
                    message: '请选择父分类'
                }
            }
        },
        proportion: {
            validators: {
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: '比重只能输入正整数'
                },
                notEmpty: {
                    message: '比重不能为空'
                }
            }
        }
    }
};
/**
 * 验证数据是否为空
 */
QuestionCategoryInfoDlg.validate = function () {
    $('#questionCategoryInfoForm').data("bootstrapValidator").resetForm();
    $('#questionCategoryInfoForm').bootstrapValidator('validate');
    return $("#questionCategoryInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 清除数据
 */
QuestionCategoryInfoDlg.clearData = function () {
    this.questionCategoryInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
QuestionCategoryInfoDlg.set = function (key, val) {
    this.questionCategoryInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
QuestionCategoryInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
QuestionCategoryInfoDlg.close = function () {
    parent.layer.close(window.parent.QuestionCategory.layerIndex);
}

/**
 * 收集数据
 */
QuestionCategoryInfoDlg.collectData = function () {
    this
        .set('id')
        .set('pid')
        .set('pids')
        .set('name')
        .set('proportion')
        .set('available')
        .set('remarks');
}

/**
 * 提交添加
 */
QuestionCategoryInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/questionCategory/add", function (data) {
        Feng.success("添加成功!");
        window.parent.QuestionCategory.table.refresh();
        QuestionCategoryInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.questionCategoryInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
QuestionCategoryInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/questionCategory/update", function (data) {
        Feng.success("修改成功!");
        window.parent.QuestionCategory.table.refresh();
        QuestionCategoryInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.questionCategoryInfoData);
    ajax.start();
};
QuestionCategoryInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#pidSel").attr("value", instance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
};

/**
 * 显示部门选择的树
 *
 * @returns
 */
QuestionCategoryInfoDlg.showCategorySelectTree = function () {
    var pidObj = $("#pidSel");
    var pidPostion = $("#pidSel").offset();
    $("#menuContent").css({
        left: pidPostion.left + "px",
        top: pidPostion.top + pidObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 显示用户详情部门选择的树
 *
 * @returns
 */
QuestionCategoryInfoDlg.showInfoCategorySelectTree = function () {
    var pidObj = $("#pidSel");
    var pidPostion = $("#pidSel").position();
    $("#menuContent").css({
        left: pidPostion.left + "px",
        top: pidPostion.top + pidObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
QuestionCategoryInfoDlg.hideCategorySelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
            event.target).parents("#menuContent").length > 0)) {
        QuestionCategoryInfoDlg.hideDeptSelectTree();
    }
}
QuestionCategoryInfoDlg.hideDeptSelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

$(function () {
    Feng.initValidator("questionCategoryInfoForm", QuestionCategoryInfoDlg.validateFields);

    var ztree = new $ZTree("treeDemo", "/questionCategory/tree");
    ztree.bindOnClick(QuestionCategoryInfoDlg.onClickDept);
    ztree.init();
    instance = ztree;
});
